package com.servlet;

import com.beans.CateInfo;
import com.dao.CateDao;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/CateServlet.do")
public class CateServlet extends HttpServlet {
    CateDao cateDao=new CateDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String flag=request.getParameter("flag");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
       if("addBigCate".equals(flag)){
           addBigCate(request,response);
       }
       else if("addSmallCate".equals(flag)){
           addSmallCate(request,response);
       }
       else if("manage".equals(flag)){
           manage(request,response);
       }
       else if("updateBigCate".equals(flag)){
           updateBigCate(request,response);
       }
       else if("delbigcate".equals(flag)){
           delbigcate(request,response);
       }
       else if("updateSmallCate".equals(flag)){
           updateSmallCate(request,response);
       }
       else if("delsmallcate".equals(flag)){
           delsmallcate(request,response);
       }
       else if("listSmallcate".equals(flag)){
           listSmallcate(request,response);
       }
    }

    private void listSmallcate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int bigCateId=Integer.parseInt(request.getParameter("bigcateid"));
        List<CateInfo> cateList=cateDao.getCateList(bigCateId);

        JSONArray jsonObj=JSONArray.fromObject(cateList);
        response.getWriter().print(jsonObj);
    }

    //删除小分类
    private void delsmallcate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id= Integer.parseInt(request.getParameter("id"));
        int result=cateDao.delCateById(id);
        if(result==1){
            request.setAttribute("msg","二级分类删除成功");
            manage(request,response);
        }
    }

    //修改小分类
    private void updateSmallCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        Integer parentId=Integer.parseInt(request.getParameter("bigCateId"));
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        CateInfo cateInfo=new CateInfo();
        cateInfo.setId(id);
        cateInfo.setCateName(cateName);
        cateInfo.setDes(des);
        cateInfo.setParentId(parentId);
        int result=cateDao.updateCate(cateInfo);
        if(result==1){
            request.setAttribute("cateInfo",cateInfo);
            request.setAttribute("msg","保存成功");
            request.getRequestDispatcher("/cate/smallcate_edit.jsp").forward(request,response);
        }
    }

    //删除大分类
    private void delbigcate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer cateId=Integer.parseInt(request.getParameter("id"));
        int smallCateCount=cateDao.getSubCountByBigCate(cateId);
        if(smallCateCount==0){
            if(cateDao.delCateById(cateId)==1){
                request.setAttribute("msg", "一级分类删除成功!");
            }
        }
        else{
            request.setAttribute("msg","操作失败! 该分类下尚有 "+smallCateCount+" 个子分类,所以不能进行删除!");
        }
        manage(request, response);
    }
    //更新大分类
    private void updateBigCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        CateInfo cateInfo=new CateInfo();
        cateInfo.setId(id);
        cateInfo.setCateName(cateName);
        cateInfo.setDes(des);
        cateInfo.setParentId(0);
        int result=cateDao.updateCate(cateInfo);
        if(result==1){
            request.setAttribute("cateInfo",cateInfo);
            request.setAttribute("msg","保存成功");
            request.getRequestDispatcher("/cate/bigcate_edit.jsp").forward(request,response);
        }
    }
    //分类管理
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<CateInfo> cateList=cateDao.getCateList(0);
        request.setAttribute("cateList",cateList);
        request.getRequestDispatcher("/cate/cate_manage.jsp").forward(request,response);
    }
    //添加小分类
    private void addSmallCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer bigCateId=Integer.parseInt(request.getParameter("bigcateId"));
        String smallCateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        CateInfo cateInfo=new CateInfo();
        cateInfo.setCateName(smallCateName);
        cateInfo.setDes(des);
        cateInfo.setParentId(bigCateId);
        int result=cateDao.addCate(cateInfo);
        if(result==1){
            request.setAttribute("msg", "二级分类添加成功");
            request.getRequestDispatcher("/cate/smallcate_add.jsp").forward(request, response);
        }

    }
    //添加一级分类
    private void addBigCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        CateInfo cateInfo=new CateInfo();
        cateInfo.setCateName(cateName);
        cateInfo.setDes(des);
        cateInfo.setParentId(0);
        int result=cateDao.addCate(cateInfo);
        if(result==1){
            request.setAttribute("msg","一级分类添加成功");
            request.getRequestDispatcher("/cate/bigcate_add.jsp").forward(request,response);
        }
    }
}
